﻿

namespace Tentacle.Core
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.Net.Http;
    using Tentacle.Downloader;
    using System.Xml.Serialization;

    using static System.String;
    using static Tentacle.Utils.Hepler;
    

    public class WebToken
    {
        /// <summary>
        /// 
        /// </summary>        
        [XmlElement("Proxy")]
        public string Proxy { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [XmlElement("Cookie")]
        public KeyValuePair<string,string>[] Cookie { get; set; }

        #region Helper Method

        public static WebToken Parse(string token)
        {
            return new WebToken();
        }

        internal void SetToken(RemotedHttpClientHandler handler)
        {
            // Set Proxy
            if (!IsNullOrEmpty(Proxy)) {
                try {
                    handler.Proxy = new WebProxy(Proxy);
                    handler.UseProxy = true;
                }
                catch(Exception ex) {
                    Trace(ex);
                }
            }

            // Set Cookie
            if (Cookie != null) {
                handler.UseCookies = true;
                handler.CookieContainer = new CookieContainer(Cookie.Length);
                foreach(var cookie in Cookie) {
                    handler.CookieContainer.Add(new Cookie(cookie.Key,cookie.Value, "/", "/"));
                }
            }
        }        

        #endregion
    }
}
